package com.bilin.coin.tools;

import com.bilin.coin.ApiConstant;
import com.bilin.coin.common.base.BaseAppCompatActivity;
import com.bilin.coin.network.RequestUtils;
import com.bilin.coin.network.callback.CommonCallBack;
import com.bilin.coin.utils.DialogUtils;
import com.trello.rxlifecycle2.android.ActivityEvent;

import java.net.SocketTimeoutException;
import java.util.Map;



public abstract class Poster {

    BaseAppCompatActivity appCompatActivity;
    boolean isShowError = true;
    boolean isShowSuccess = true;
    boolean isShowDialog = true;

    public Poster(BaseAppCompatActivity appCompatActivity) {
        this.appCompatActivity = appCompatActivity;
        post();
    }

    public Poster(BaseAppCompatActivity appCompatActivity, boolean isShowError) {
        this.appCompatActivity = appCompatActivity;
        this.isShowError = isShowError;
        post();
    }

    public Poster(BaseAppCompatActivity appCompatActivity, boolean isShowError, boolean isShowSuccess) {
        this.appCompatActivity = appCompatActivity;
        this.isShowError = isShowError;
        this.isShowSuccess = isShowSuccess;
        post();
    }

    public Poster(BaseAppCompatActivity appCompatActivity, boolean isShowError, boolean isShowSuccess, boolean isShowDialog) {
        this.appCompatActivity = appCompatActivity;
        this.isShowError = isShowError;
        this.isShowSuccess = isShowSuccess;
        this.isShowDialog = isShowDialog;
        post();
    }

    private void post() {
        if (isShowDialog) {
            DialogUtils.showDialogLoading(appCompatActivity);
        }
        RequestUtils
                .post()
                .url(fillUrl())
                .params(fillParams())
                .build()
                .compose(appCompatActivity.<String>bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new CommonCallBack() {

                    @Override
                    protected void onError(String errorMsg) {
                        DialogUtils.dismissDialogLoading();
                        if (isShowError && !errorMsg.endsWith(ApiConstant.NO_PASS_TAIL)&& !errorMsg.endsWith(ApiConstant.NO_AUTH_TAIL)) {
                            appCompatActivity.showToast(errorMsg);
                        }
                        disposeError(errorMsg);
                    }
                    @Override
                    protected void onSuccess(String dataJson) {
                        DialogUtils.dismissDialogLoading();
                        disposeSuccess(dataJson);
                        if (isShowSuccess)  {
                            appCompatActivity.showToast("操作成功");
                        }
                    }
                    @Override
                    protected void onCallBackError(Throwable e) {
                        DialogUtils.dismissDialogLoading();
                        if (e instanceof SocketTimeoutException) {
                            appCompatActivity.showToast("网络开小差了");
                        } else {
                            appCompatActivity.showMsg(e);
                        }
                    }
                });
    }

    protected void disposeError(String errorMsg) {


    }

    /**
     * 子类重写
     * @param
     */
    protected void disposeStopFresh() {

    }
    /**
     * 子类重写
     * @param dataJson
     */
    protected void disposeSuccess(String dataJson) {

    }

    protected abstract String fillUrl();

    protected abstract Map<String, String> fillParams();
}
